package com.ssy.invoicing.order.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ssy.invoicing.dao.support.BaseSqlDao;
import com.ssy.invoicing.order.bean.PurchaseOrderBean;
import com.ssy.invoicing.order.bean.PurchaseProductBean;

@Repository(value = "purchaseDao")
public class PurchaseDao extends BaseSqlDao {
    
    /**
     * 新增采购订单信息
     * @param purchase
     */
    public PurchaseOrderBean savePurchase(PurchaseOrderBean purchase) {
        insert("purchase.insertPurchase", purchase);
        return purchase;
    }
    
    /**
     * 根据Id获取采购订单信息
     * @param purchaseId
     * @return
     */
    public PurchaseOrderBean getPurchaseById(Integer purchaseId) {
        return (PurchaseOrderBean) queryForObject("purchase.getPurchaseDetail", purchaseId);
    }
    
    /**
     * 根据采购订单Id加载产品 
     * @author suoyao.shen
     * @param orderId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<PurchaseProductBean> loadProductByPurchaseId(Integer purchaseId) {
        return (List<PurchaseProductBean>) queryForList("purchase.queryProductByPurchaseId", purchaseId);
    }

    /**
     * 批量保存采购订单产品信息
     * @author suoyao.shen
     * @param productList
     */
    @SuppressWarnings("rawtypes")
    public void savePurchaseProductBatch(final List<PurchaseProductBean> productList) {
        executeWithListResult(new SqlMapClientCallback() {
            private final List<PurchaseProductBean> newProductList = productList;

            @Override
            public Object doInSqlMapClient(SqlMapExecutor sqlmapexecutor) throws SQLException {
                //开始批量操作
                sqlmapexecutor.startBatch();
                for (PurchaseProductBean product : newProductList) {
                    sqlmapexecutor.insert("purchase.insertPurchaseProduct", product);
                }
                sqlmapexecutor.executeBatch();
                return newProductList;
            }
        });
    }
    
    /**
     * @param params
     * @param page
     * @return
     * @throws SQLException
     */
    public SplitPageResult queryPurchaseList(Map<String, Object> params, int page) throws SQLException {
        return queryListByPagination("purchase.queryPurchaseList", "purchase.queryPurchaseCount",
                params, page);
    }
    
    /**
     * @param id
     */
    public void removePurchase(Integer id) {
        delete("purchase.removePurchase", id);
    }
    
    /**
     * 根据采购订单Id删除对应的产品信息
     * @param purchaseId
     */
    public void removePurchaseProduct(Integer purchaseId) {
        delete("purchase.removePurchaseProduct", purchaseId);
    }
    
    /**
     * 获得采购订单最大的序列号
     * @param orderId
     * @return
     */
    public Integer getPurchaseSeries(Integer orderId) {
        return (Integer) queryForObject("purchase.getPurchaseSeries", orderId);
    }
    
}
